/**
 * 
 */
package vanhoc.android.mediakool;

import java.util.ArrayList;

import vanhoc.android.mediakool.R;
import vanhoc.android.mediakool.custom.adapter.MusicListAdapter;
import vanhoc.android.mediakool.db.DatabaseHelper;
import vanhoc.android.medialcool.item.data.MusicItemData;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * @author Administrator
 *
 */
public class GenresActivity extends Activity implements OnItemClickListener {
	ArrayList<MusicItemData> arrListGenres ;
	
	ListView listViewGenre;
	
	//id bai hat dang hat, mac dinh chua co bai hat nao hat thi gan  = 0
	int songPlayingID = 0;
		
	DatabaseHelper databaseHelper;
		
	private final static String FIELDNAME = "genre";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.genres);

		databaseHelper = new DatabaseHelper(GenresActivity.this);
		
		//Khi dang choi nhac o min hinh MusicPlayerActivity, user back lai man hinh TabControlActivity se gui qua Intent id cua bai hat dang choi
		//Con khi moi mo ung dung len(tuc la chua choi nha) thi khong co Intent
		//lay ve id cua bai hat dang choi quan intent (neu co) -> them icon cho biet la co 1 bai hat dang hat trong album nao
		this.songPlayingID	=	 getIntent().getExtras().getInt("songPlayingID");
		
		
		listViewGenre	=	(ListView) findViewById(R.id.listGenre);
		
		DatabaseHelper databaseHelper = new DatabaseHelper(GenresActivity.this);
		databaseHelper.openToRead();
		arrListGenres	=	databaseHelper.getAllItemDataByFieldName(FIELDNAME);
		databaseHelper.close();
		
		
		MusicListAdapter adapter = new MusicListAdapter(getApplicationContext(), R.id.listGenre, arrListGenres);
		listViewGenre.setAdapter(adapter);
		
		if (this.songPlayingID != 0) {
			String title = this.getGenreTitle(this.songPlayingID);
			adapter.setGenreTitle(title);
		}
		
		listViewGenre.setOnItemClickListener(this);
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		// TODO Auto-generated method stub
		Intent it = new Intent(getApplicationContext(), SongsActivity.class);
		
		//put key : tuc la ten field trogn song table (de biet dc lay ds bai hat cua 1 genre, hay 1 album, hay 1 playlist nao)
		it.putExtra("FIELDNAME", FIELDNAME);
		
		//put value : tuc la ten field can lay ( vidu lay ds bai hat cua Genre co title la POP)
		it.putExtra("FIELDVALUE", arrListGenres.get(position).getTitle());
		
		it.putExtra("songPlayingID", GenresActivity.this.songPlayingID);
		startActivity(it);
	}
	
	//Lay ve ten cua album chua bai hat dang hat
	public String getGenreTitle(int songID){
		databaseHelper.openToRead();
		Cursor cursor	=	databaseHelper.getInfoSong(this.songPlayingID);
		cursor.moveToFirst();
		String genreTitle = null;
		for (int i = 0; i < cursor.getCount(); i++) {
			genreTitle = cursor.getString(6);
 		}
		databaseHelper.close();
		
		return genreTitle;
	}
	
	@Override
	public void onBackPressed() {
		Intent it = new Intent(getApplicationContext(), DashboardActivity.class);
    	startActivity(it);
	}
	
}
